package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.*;
import com.itheima.service.RoleService;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @ClassName RoleController
 * @Description
 * @Author Mr.Ho
 * @Date 9:39 2019/7/17
 * @Version 1.1
 **/

@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;


    //分页查询记录
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult<Role> pageList = null;
        try {
            pageList = roleService.findPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }

    }


    //新增记录
    @RequestMapping("/add")
    public Result add(@Param("permissionIds") Integer[] permissionIds, @Param("menuIds") Integer[] menuIds, @RequestBody Role role){
        try {
            roleService.add(permissionIds,menuIds,role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
        return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
    }

    //根据id查询
    @RequestMapping("/findById")
    public Result findById(Integer id){
        Role role = roleService.findById(id);
        if(role!=null)
        {
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,role);
        }
        return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
    }

    //根据id删除记录
    @RequestMapping("/deleteById")
    public Result deleteById(int id){
        roleService.deleteById(id);
        return new Result(true,MessageConstant.DELETE_ROLE_SUCCESS);
    }


    //编辑角色
    @RequestMapping("/edit")
    public Result edit(@Param("permissionIds") Integer[] permissionIds, @Param("menuIds") Integer[] menuIds, @RequestBody Role role){
        try {
            roleService.edit(permissionIds,menuIds,role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
    }


    //查询所有列表
    @RequestMapping("/findAll")
    public Result findAll(){
        List<Role> roleList = roleService.finAll();
        if(roleList!=null&&roleList.size()!=0)
        {
            Result result = new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,roleList);
            return result;
        }else {
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }


    //根据user的id查找
    @RequestMapping("/findRoleIdsByUserId")
    public List<Integer> findRoleIdsByUserId(int id){
        List<Integer> list = roleService.findRoleIdsByUserId(id);
        return list;
    }

}
